package com.ys.controller.api.hr;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.hr.TwlStockFrozenRunning;
import com.ys.entity.hr.TwlStockIn;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/twlStockFrozenRunning")
public class ApiInterTwlStockFrozenRunningController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TwlStockFrozenRunning> commonApiService;
	
	@RequiresPermissions("twlStockFrozenRunning:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TwlStockFrozenRunning cond){
		
		cond = setSeachSql(cond);
		
		cond.setOrderby("A.Id DESC");
		return commonApiService.searchLay(cond);
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("twlStockFrozenRunning:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TwlStockFrozenRunning cond) throws Exception{
		
		cond = setSeachSql(cond);
		
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TWL_StockFrozenRunning)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TwlStockIn> util = new ExportExcelUtils<TwlStockIn>(new TwlStockIn());
        
        String toFilePath = util.exportExcel(listData, listField, "物料库存冻结记录");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TwlStockFrozenRunning setSeachSql(TwlStockFrozenRunning cond){
		
		cond.addLeftJoin("TWL_Stock B on B.Id = A.StockId");
		
		cond.addLeftJoin("TB_Material C on C.Code = B.MaterialCode")
			.addField("C.Code materialCode")
			.addField("C.Name materialName")
			.addField("C.Remark materialRemark");
        return  cond;
    }
}
